package com.wuhainan.beelzebub.service.system;

import com.wuhainan.beelzebub.common.BaseService;
import com.wuhainan.beelzebub.entity.system.CategoryDetails;
import com.wuhainan.beelzebub.repository.system.CategoryDetailsRepository;
import com.wuhainan.beelzebub.repository.system.ChargeItemTypeRepository;
import com.wuhainan.beelzebub.utils.PageInfo;
import com.wuhainan.beelzebub.utils.TableData;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <br>Created by 吴海南 on 2017-12-18.
 * <br>星期一 at 下午 3:05.
 */
@Service
public class CategoryDetailsService extends BaseService<CategoryDetails>{
    @Resource
    private CategoryDetailsRepository repository;
    @Resource
    private ChargeItemTypeRepository citRepository;
    public List<CategoryDetails> findAll() {
        Sort sort=new Sort(Sort.Direction.ASC,"sort");
        return repository.findAll(sort);
    }

    public TableData findCategoryDetailsPage(PageInfo pageInfo, CategoryDetails categoryDetails) {
        Page<CategoryDetails> page=repository.findAll((root, cq, cb) -> {
            search(categoryDetails, cq, cb, root);//模糊查询
            return cq.getRestriction();
        },pageable(pageInfo,sort(pageInfo, Sort.Direction.ASC,"sort")));
        return tableData(page);
    }
    public CategoryDetails findCategoryDetailsById(String id) {
        return repository.findOne(id);
    }

    /**
     * 保存类别明细
     *
     * @param categoryDetails 类别明细
     */
    public void saveCategoryDetails(CategoryDetails categoryDetails) {
        categoryDetails.setChargeItemTypeId(citRepository.findByTypeName(categoryDetails.getTypeName()).getId());
        categoryDetails.setType(citRepository.findByTypeName(categoryDetails.getTypeName()).getType());
        repository.save(categoryDetails);
    }

    /**
     * 删除类别明细
     *
     * @param categoryDetails 类别明细
     */
    public void deleteCategoryDetails(CategoryDetails categoryDetails) {
        repository.delete(categoryDetails);
    }

}
